package riven.club.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.Map;

/**
 * @author 陈浩
 * @date 2021-10-18 14:43
 */
@Controller
//@RequestMapping("/api")
public class UomExtendController {

//    @GetMapping("/page/resources")
//    public Object user(String ticketId, String cs) {
////        System.out.println("认证");
//        JSONObject jsonObject = new JSONObject();
////        jsonObject.put()
//        System.out.println("生成票据");
//        System.out.println(ticketId);
//        return jsonObject.toJSONString();
//    }

    @GetMapping("/page1")
    public String page1() {
        return "hello.html";
    }

    @GetMapping("/page2")
    public String page2() {
        return "page2";
    }

    @GetMapping("/api/page/resources")
    public String resourceLocation(@RequestParam Map param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", "操作成功");
        jsonObject.put("success", true);

        String id = param.containsKey("id") ? String.valueOf(param.get("id")) : "";
        if (StringUtils.isEmpty(id)) {
            JSONArray JsonArray = new JSONArray();

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("id", "id-test-01");
            jsonObject1.put("title", "第三方菜单资源一");
            jsonObject1.put("location", "/page1");
            jsonObject1.put("nodeType", "GROUP");
            jsonObject1.put("data", null);
            jsonObject1.put("description", null);
            jsonObject1.put("leaf", false);
            jsonObject1.put("resourceType", "type-test-01");
            JsonArray.add(jsonObject1);

            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("id", "id-test-02");
            jsonObject2.put("title", "第三方菜单资源二");
            jsonObject2.put("location", "/page2");
            jsonObject2.put("nodeType", "GROUP");
            jsonObject2.put("data", null);
            jsonObject2.put("description", null);
            jsonObject2.put("leaf", false);
            jsonObject2.put("resourceType", "type-test-02");
            JsonArray.add(jsonObject2);

            jsonObject.put("pages", JsonArray);

        } else {

            if ("id-test-01".equals(id)) {
                JSONArray JsonArray = new JSONArray();

                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("id", "4c350980-b4ad-4c59-b69b-beb90676f370");
                jsonObject1.put("title", "测试页面01");
                jsonObject1.put("location", "/sso?page=test01");
                jsonObject1.put("nodeType", "PAGE");
                jsonObject1.put("description", null);
                jsonObject1.put("leaf", true);
                jsonObject1.put("resourceType", "type-page-01");
                JsonArray.add(jsonObject1);

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("id", "55b3e121-7091-493d-9e16-f146f56e3ace");
                jsonObject2.put("title", "测试页面02");
                jsonObject2.put("location", "/sso?page=test02");
                jsonObject2.put("nodeType", "PAGE");
                jsonObject2.put("description", null);
                jsonObject2.put("leaf", true);
                jsonObject2.put("resourceType", "type-page-02");
                JsonArray.add(jsonObject2);

                jsonObject.put("pages", JsonArray);
            } else if ("id-test-02".equals(id)) {
                JSONArray JsonArray = new JSONArray();

                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("id", "4c350980-b4ad-4c59-b69b-beb90676f270");
                jsonObject1.put("title", "测试页面03");
                jsonObject1.put("location", "/sso?page=test03");
                jsonObject1.put("nodeType", "PAGE");
                jsonObject1.put("description", null);
                jsonObject1.put("leaf", true);
                jsonObject1.put("resourceType", "type-page-03");
                JsonArray.add(jsonObject1);

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("id", "55b3e121-7091-493d-9e16-f146f56e33ce");
                jsonObject2.put("title", "测试页面04");
                jsonObject2.put("location", "/sso?page=test04");
                jsonObject2.put("nodeType", "PAGE");
                jsonObject2.put("description", null);
                jsonObject2.put("leaf", true);
                jsonObject2.put("resourceType", "type-page-04");
                JsonArray.add(jsonObject2);

                jsonObject.put("pages", JsonArray);
            } else {
                throw new RuntimeException();
            }

        }

        return jsonObject.toJSONString();
    }

    /**
     * 加密规则1(申请票据)
     */
    @Test
    public void riven1Test() {

        String result = "";
        String userName = "admin";
        String clientId = "c7543146-475c-2c1e-9b1e-4169260427d1";
        String clientSecret = "68691a76-c8dd-1a82-2194-a7ec36192e35";
        Charset utf8 = Charset.forName("utf8");
        long now = System.currentTimeMillis();
        //MD5(clientid:time:userName:clientsecret)
        String md5Text = String.format("%s:%s:%s:%s", clientId, now, userName, clientSecret);
        String md5 = MD5(md5Text);
        String authorizationText = String.format("%s:%s:%s:%s", clientId, now, userName, md5);
        byte[] authorizationByte = authorizationText.getBytes(utf8);
        result =
                new String(Base64.getEncoder().encode(authorizationByte), utf8);

        System.out.println("result");
        System.out.println(result);
    }

    @Test
    public void riven2Test() {

        System.out.println(Base64("", "899a965ff6f74f9c93f825a9f2efb36e"));
    }

    private static String MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    // java示例代码  调用 Base64("NVWA", "c4c0295d0e7c499ebc36813637cfeb16")
    private static String Base64(String code, String value) {
        Charset utf8 = Charset.forName("utf8");
        //Base64(code:value)
        String authorizationText = String.format("%s:%s", code, value);
        byte[] authorizationByte = authorizationText.getBytes(utf8);
        return new String(Base64.getEncoder().encode(authorizationByte), utf8);
    }
}
